<?php

use yii\helpers\Html;
use yii\helpers\Url;
/* @var $this yii\web\View */

$this->title = '行为日志';
$pageSize = \Yii::$app->params['pageSize'];
$limitsJson = \Yii::$app->params['limitsJson'];
?>
<!-- 面包屑 -->
<?= \Yii::$app->view->renderFile('@app/views/public/breadcrumb.php') ?>
<!-- 面包屑 -->
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body ">
                    <div class="demoTable" style="margin: 1px 0;margin-left: 0.5%;">
                    <form class="layui-form" action="">                           
                            <style>
                                .where-item{margin-bottom: 0.6em;}
                                .where-item-input{width: 168px; margin-right: 10px;}
                            </style>
                            <div class="layui-inline layui-show-xs-block where-item">
                                <label>查询条件：</label>
                                <div class="layui-inline layui-show-xs-block  where-item-input">
                                <input class="layui-input" name="parames" id="parames" autocomplete="off" placeholder="请输入">
                                </div>

                            </div>


                            <div class="layui-inline layui-show-xs-block where-item">
                                <label>访问者：</label>
                                <div class="layui-inline layui-show-xs-block  where-item-input">
                                    <div id="username"> </div>
                                </div>

                            </div>


                            <div class="layui-inline  layui-show-xs-block where-item">
                                <label>SESSION：</label>
                                <div class="layui-inline layui-show-xs-block  where-item-input" >
                                    <input class="layui-input" name="session_id" id="session_id" autocomplete="off" placeholder="请输入">
                                </div>

                            </div>


                            <div class="layui-input-inline layui-show-xs-block where-item">
                                <label>应用类型：</label>
                                <div class="layui-inline layui-show-xs-block  where-item-input">
                                    <select name="app_id" id="app_id" lay-search="">
                                        <option value="">请选择</option>

                                        <option value="app-frontend">app-frontend</option>
                                        <option value="app-backend">app-backend</option>
                                        <option value="api">API</option>
                                    </select>
                                </div>

                            </div>


                            <div class="layui-input-inline layui-show-xs-block where-item">
                                <label>行为类型：</label>
                                <!-- <input class="layui-input" name="parames" id="parames" autocomplete="off" placeholder="请输入搜索条件"> -->
                                <div class="layui-inline layui-show-xs-block where-item-input">
                                    <select name="behavior" id="behavior" lay-search="">
                                        <option value="">请选择</option>
                                        <option value="1">页面浏览</option>
                                        <option value="2">查询数据</option>
                                        <option value="3">查询详情</option>
                                        <option value="4">增加数据</option>
                                        <option value="5">修改数据</option>
                                        <option value="6">删除数据</option>
                                        <option value="0">其他操作</option>

                                    </select>
                                </div>


                            </div>


                            <div class="layui-inline layui-show-xs-block where-item">
                                <label>开始时间：</label>
                                <!-- <input class="layui-input" name="parames" id="parames" autocomplete="off" placeholder="请输入搜索条件"> -->
                                <div class="layui-inline layui-show-xs-block where-item-input" >
                                    <input class="layui-input" autocomplete="off" readonly placeholder="请选择" name="start" id="start">
                                </div>

                            </div>

                            <div class="layui-inline layui-show-xs-block where-item">
                                <label>结束时间：</label>
                                <!-- <input class="layui-input" name="parames" id="parames" autocomplete="off" placeholder="请输入搜索条件"> -->
                                <div class="layui-inline layui-show-xs-block where-item-input" >
                                    <input class="layui-input" autocomplete="off" readonly placeholder="请选择" name="end" id="end">
                                </div>

                            </div>
                            <div class="layui-inline layui-show-xs-block where-item">
                                <button class="layui-btn" type="button" id="sreach" data-type="sreach" lay-filter="sreach">
                                    <i class="layui-icon">&#xe615;</i></button>
                            </div>
                            <!--  <a title="搜索" class="layui-btn" data-type="reload"><i class="layui-icon" style="font-size: 20px;">&#xe615;</i></a> -->
                        </form>
                    </div>
                    <script type="text/html" id="topToolbar">
                        <div class="layui-btn-container">
                            <button class="layui-btn layui-btn-danger layui-btn-xs" lay-event="getCheckData">批量删除</button>
                        </div>
                    </script>
                    <div class="layui-card-body ">
                        <table class="layui-table" id="dataTable" lay-filter="dataTable">
                        
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<input name="_csrf" type="hidden" id="_csrf" value="<?= Yii::$app->request->getCsrfToken() ?>">
<!-- 操作列 -->
<script type="text/html" id="auth_state">
    <!-- <a class="layui-btn layui-btn-xs" lay-event="view"><i class="layui-icon">&#xe63c;</i> </a> -->
    <a class="layui-btn layui-btn-xs" lay-event="view">查看</a>
</script>

<script>
    $(function() {
        layui.use('laydate',
            function() {
                var laydate = layui.laydate;
                //执行一个laydate实例
                laydate.render({
                    elem: '#start' //指定元素
                });
                //执行一个laydate实例
                laydate.render({
                    elem: '#end' //指定元素
                });
            });
    })

    var xmUsername = xmSelect.render({
        el: '#username',
        language: 'zn',
        size: 'mini',
        tips: '请选择',
        searchTips: '输入姓名进行查询',
        style: {
            minHeight: '30px',
        },
        model: {
            label: {
                type: 'block',
                block: {
                    //最大显示数量, 0:不限制
                    showCount: 1,
                    //是否显示删除图标
                    showIcon: false,
                }
            },
        },

        autoRow: true,
        toolbar: {
            show: true
        },
        filterable: true,
        remoteSearch: true,
        prop: {
            name: 'username',
            value: 'user_id',
        },

        remoteMethod: function(val, cb, show) {
            var _this = xmUsername;
            //这里如果val为空, 则不触发搜索
            if (!val) {
                var data = [];
                if (xmUsername != undefined) {
                    data = xmUsername.getValue();
                }
                return cb(data);
            }

            Cajax({
                type: "POST",
                url: "<?php echo \yii\helpers\Url::toRoute(['/auth/get-admin-list']); ?>",
                data: {
                    _csrfBackend: '<?= \Yii::$app->request->csrfToken ?>',
                    username_search: val
                }
            }, function() {
                cb([]);
            }, function(JsonData) {
                //console.log(xmUsername);
                var res = JsonData;
                data = res.data;
                cb(data);


            })
        },
    })
</script>

<?php

$listUrl = \yii\helpers\Url::toRoute(['list']);
$viewUrl = \yii\helpers\Url::toRoute(['view']);
$updateUrl = \yii\helpers\Url::toRoute(['update']);
$delUrl = \yii\helpers\Url::toRoute(['delete']);
$addUrl = \yii\helpers\Url::toRoute(['create']);
$_csrfBackend = \Yii::$app->request->csrfToken;
$this->registerJsFile('@web/js/xm-select.js', ['position' => \yii\web\View::POS_BEGIN]);

$tableJs = <<<JS

    function rowDel(obj,data,index) {
            if(data.length == 0){
                layer.msg('请选择要删除的数据', {icon: icon.ICON_WARING});
                return false;
            }
            var id = (data.length >= 1) ? getColumn(data,'id') : data.id;
            layer.confirm('真的删除吗？', function(index){
                //var url = "$delUrl";
                //var urlArgs = {id:id} ; 
                //xadmin.open('修改配置',attachUrlArgs(url,urlArgs),500,550);
                //console.log(id);

                Cajax({
                    type:"POST",
                    url:"$delUrl",
                    data:{_csrfBackend:'$_csrfBackend',id:id}
                },function(){},function(JsonData){
                    layer.msg(JsonData.msg,{icon:(JsonData.status) ? icon.ICON_OK : icon.ICON_ERROR});
                    if(JsonData.status){
                        //obj.del();
                        obj.reload('dataTable', {
                            page: {
                                curr: 1 //重新从第 1 页开始
                            }
                        }, 'data');
                        layer.close(index);
                    }
                })

            });
       }
    function delSelected(obj,data,index){
        if(data.length == 0){
                layer.msg('请选择要删除的数据', {icon: icon.ICON_WARING});
                return false;
            }
        var id = (data.length > 0) ? getColumn(data,'id') : data.id;

        layer.confirm('真的删除吗？', function(index){
            //obj.del();
            console.log(obj);
            console.log(data);
            console.log(index);
            //layer.close(index);

            var id = (data.length >= 1) ? getColumn(data,'id') : data.id;
            layer.confirm('真的删除吗？', function(index){
                //var url = "$delUrl";
                //var urlArgs = {id:id} ; 
                //xadmin.open('修改配置',attachUrlArgs(url,urlArgs),500,550);

                Cajax({
                    type:"POST",
                    url:"$delUrl",
                    data:{_csrfBackend:'$_csrfBackend',id:id}
                },function(){},function(JsonData){
                    layer.msg(JsonData.msg,{icon:(JsonData.status) ? icon.ICON_OK : icon.ICON_ERROR});
                    if(JsonData.status){
                        obj.del();
                        layer.close(index);
                    }
                })
            });


        })
    }
    layui.use(['table','util'],function() {
        var table = layui.table,
            form = layui.form;
        //var util = layui.util;
        var renderOpt = {
            id:'dataTable',
            elem:'#dataTable',
            toolbar:"#topToolbar",
            url: '$listUrl', //数据接口
            method:"POST", //
            where:{_csrfBackend:'$_csrfBackend'},
            page: true, //开启分页
            limit: $pageSize,
            limits: $limitsJson,
            request:{
                limitName: 'pageSize' //每页数据量的参数名，默认：limit
            },
            cols : [[
                {type: 'checkbox', fixed: 'left'},                      
                {field: 'user_id', title: '用户ID', width: '120', align:'center'},            
                /* {field: 'behavior', title: '行为类别', width: '120', align:'center',templet:function(d){
                    if(d.behavior == 1){
                        return "页面浏览"
                    }else if(d.behavior == 2){
                        return "查询数据"
                    }else if(d.behavior == 3){
                        return "查询详情"
                    }else if(d.behavior == 4){
                        return "添加数据"
                    }else if(d.behavior == 5){
                        return "修改数据"
                    }else if(d.behavior == 6){
                        return "删除数据"
                    }else if(d.behavior == 0){
                        return "其他操作"
                    }
                }},  */
                {field: 'session_id', title: 'session_id', width: '120', align:'center'},             
                {field: 'method', title: '提交方式', width: '120', align:'center'},   
                {field: 'app_id', title: '应用ID', width: '120', align:'center'},            
                {field: 'module', title: '模块', width: '120', align:'center'},            
                {field: 'controller', title: '控制器', width: '120', align:'center'},            
                {field: 'action', title: '方法', width: '120', align:'center'},            
                {field: 'url', title: '地址', width: '120', align:'center'},                       
                {field: 'ip', title: 'Ip', width: '120', align:'center'},                                
                /* {field: 'status', title: '类型', width: '120', align:'center',templet:function(d){
                    if(d.status == 1){
                        return "前台"
                    }else if(d.status == 2){
                        return "后台"
                    }else if(d.status == 3){
                        return "API"
                    }else if(d.status == 0){
                        return "其他"
                    }
                    
                }},  */                            
                /* {field: 'remark', title: '日志备注', width: '200', align:'center'}, */
                {field: 'get_data', title: 'get数据',templet:'<div>{{d.get_data}}</div>',minWidth: 80},
                {field: 'post_data', title: 'post数据',templet:function(d){ 
                        let str = d.post_data;
                        return '<div>'+ str +'</div>';
                    },minWidth: 80},
                {field: 'user_agent', title: 'UA信息',minWidth: 80},
                {field: 'created_at', title: '添加时间', width: '200', align:'center'},                      
                {title: '操作', width:80, templet:'#auth_state',fixed:"right", align:"center"}
            ]]
        };
        table.render(renderOpt);
        //头工具栏事件
        table.on('toolbar(topToolbar)',
            function(obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                switch (obj.event) {
                    case 'getCheckData':
                        var data = checkStatus.data;
                        layer.alert(JSON.stringify(data));
                        break;
                    case 'getCheckLength':
                        var data = checkStatus.data;
                        layer.msg('选中了：' + data.length + ' 个');
                        break;
                    case 'isAll':
                        layer.msg(checkStatus.isAll ? '全选': '未全选');
                        break;
                    case 'delAll':
                        var data = checkStatus.data;
                        /* rowDel(obj,data); */

                        if(data.length == 0){
                            layer.msg('请选择要删除的数据', {icon: icon.ICON_WARING});
                            return false;
                        }
                        var id = (data.length >= 1) ? getColumn(data,'id') : data.id;
                        layer.confirm('真的删除吗？', function(index){
                            //var url = "$delUrl";
                            //var urlArgs = {id:id} ; 
                            //xadmin.open('修改配置',attachUrlArgs(url,urlArgs),500,550);
                            console.log(obj);
                            /* console.log(id);
                            console.log(obj);
                            console.log(data);
                            console.log(index);
                            console.log(table); */
                            //console.log(obj.config.page.curr);
                            
                            Cajax({
                                type:"POST",
                                url:"$delUrl",
                                data:{_csrfBackend:'$_csrfBackend',id:id}
                            },function(){},function(JsonData){
                                layer.msg(JsonData.msg,{icon:(JsonData.status) ? icon.ICON_OK : icon.ICON_ERROR});
                                if(JsonData.status){
                                    //obj.del();                                    
                                    
                                    table.reload('dataTable', {  
                                        where:{
                                        _csrfBackend:'$_csrfBackend',
                                        
                                        }  ,                               
                                        page: {
                                            curr: obj.config.page.curr, //当前页重新加载
                                            pageSize:obj.config.limit,
                                        }
                                    }, 'data');
                                    layer.close(index);
                                }
                            })
                        });




                        break;
                    
                    case 'refresh':
                        //执行重载
                        table.reload('dataTable', {
                            page: {
                                curr: 1 //重新从第 1 页开始
                            }
                        }, 'data');
                        break;
                };
            });
        //监听行工具事件
        table.on('tool(dataTable)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                //rowDel(obj,data);
                if(data.length == 0){
                layer.msg('请选择要删除的数据', {icon: icon.ICON_WARING});
                return false;
                }
                var id = (data.length >= 1) ? getColumn(data,'id') : data.id;
                layer.confirm('真的删除吗？', function(index){
                    //var url = "$delUrl";
                    //var urlArgs = {id:id} ; 
                    //xadmin.open('修改配置',attachUrlArgs(url,urlArgs),500,550);
                    console.log(id);

                    Cajax({
                        type:"POST",
                        url:"$delUrl",
                        data:{_csrfBackend:'$_csrfBackend',id:[id]}
                    },function(){},function(JsonData){
                        layer.msg(JsonData.msg,{icon:(JsonData.status) ? icon.ICON_OK : icon.ICON_ERROR});
                        if(JsonData.status){

                            console.log(table);
                            table.reload('dataTable', {  
                                        where:{
                                        _csrfBackend:'$_csrfBackend',
                                        
                                        }  ,                               
                                        page: {
                                            //page: table.config.page.curr, //当前页重新加载
                                            page: $(".layui-laypage-skip .layui-input").val(),
                                            pageSize: $(".layui-laypage-limits").find("option:selected").val(),
                                        }
                            }, 'data');
                            layer.close(index);

                        }
                    })
                });    





            } else if(obj.event === 'edit'){
                var url = "$editUrl";
                var urlArgs = {id:data.id} ; 
                xadmin.open('修改',attachUrlArgs(url,urlArgs),500,550);

                //xadmin.open('修改用户','$addAdmin?userId='+data.user_id,500,550)
            } else if(obj.event === 'view'){
                var url = '$viewUrl';
                var urlArgs = {id:data.id} ; 
                //xadmin.open('查看',attachUrlArgs(url,urlArgs),500,550);


                layer.open({
                    type: 2,
                    offset: 't',
                    content: attachUrlArgs(url,urlArgs),
                    area:['60%','80%'],
                    title:'查看'
                });

                //xadmin.open('修改用户','$addAdmin?userId='+data.user_id,500,550)
            } else if(obj.event === 'member_stop'){
                member_stop(obj);
            }
        });
        form.on('checkbox(lockDemo)', function(obj){
            var _this = obj.othis;
            var checked = (obj.elem.checked == true) ? 1 : 0;
            Cajax({
                type:"POST",
                url:'$updateUrl',
                data:{_csrfBackend:_csrfBackend,id:this.value,is_hide_des:checked}
            },function () {},function(JsonData){
                if(JsonData.status == false){//layui-form-checked
                    (checked == 1) ? $(_this).parent('div').removeClass('layui-form-checked') : $(_this).parent('div').addClass('layui-form-checked');
                }
                layer.msg(JsonData.msg,{icon:(JsonData.status) ? icon.ICON_OK : icon.ICON_ERROR});
            })
        });

        form.on('checkbox(lockStatus)', function(obj){
            var _this = obj.othis;
            var checked = (obj.elem.checked == true) ? 1 : 0;
            Cajax({
                type:"POST",
                url:'$updateUrl',
                data:{_csrfBackend:'$_csrfBackend',id:this.value,status:checked}
            },function () {},function(JsonData){
                if(JsonData.status == false){//layui-form-checked
                    (checked == 1) ? $(_this).parent('div').removeClass('layui-form-checked') : $(_this).parent('div').addClass('layui-form-checked');
                }
                layer.msg(JsonData.msg,{icon:(JsonData.status) ? icon.ICON_OK : icon.ICON_ERROR});
            })
        });
        //搜索
         var $ = layui.$, active = {
            sreach: function(){
                var start = $('#start');
                var end = $('#end');
                var usernames = xmUsername.getValue('valueStr');
                var parames = $('#parames');
                var behavior = $('#behavior');
                var app_id = $('#app_id');
                var session_id = $('#session_id');
                //执行重载
                table.reload('dataTable', {
                    //url:'$getBaseConfigList',
                    page: { curr: 1 } ,
                    where: {
                        start: start.val(),
                        end: end.val(),
                        usernames: usernames,
                        parames:parames.val(),
                        behavior:behavior.val(),
                        app_id:app_id.val(),
                        session_id:session_id.val(),
                    }
                }, 'data');
            }
          };
          $('#sreach').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
          });
     }); 
JS;
$this->registerJs($tableJs, \yii\web\View::POS_END);
?>